
import EcoSpeed.Alarme;
import EcoSpeed.Screen;
import EcoSpeed.SmartSystem;
import EcoSpeed.Statistics;
import EcoSpeed.Utils;
import IHM.IHMSimulator;
import Simulator.DriverKeyboard;
import Simulator.DriverLift;
import Simulator.Simulator;
import SimulatorStandard.StandardDriverKeyBoard;
import SimulatorStandard.StandardDriverLift;
import TimeLine.TimeLine;
import java.util.Iterator;
import java.util.Vector;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author pierre.gillon
 */
public class Main {
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SmartSystem smartSystem;
                Simulator simu;
                TimeLine time = new TimeLine();
                Iterator it;

                Alarme alarm = new Alarme();
                alarm.setStatut(false);
                time.setRatio(1);

                // Création des DriverLift
                Vector<DriverLift> lifts = new Vector<DriverLift>();
                DriverLift dLift;
                for (int i=0; i<6; i++) {
                    dLift = new DriverLift(i);
                    lifts.add(dLift);
                }
                // Création des StandardsDriverLift
                Vector<StandardDriverLift> stdLifts = new Vector<StandardDriverLift>();
                StandardDriverLift stdLift;
                for (int i=0; i<6; i++) {
                    stdLift = new StandardDriverLift(i);
                    stdLifts.add(stdLift);
                }

                //Création des DriverKeybaord
                Vector<DriverKeyboard> keyboards = new Vector<DriverKeyboard>();
                for (int i=0; i<Utils.NB_FLOORS; i++) {
                    keyboards.add(new DriverKeyboard(i));
                }

                // Création des StandardDriverKeybaord
                Vector<Vector<StandardDriverKeyBoard>> stdKeyboards = new Vector<Vector<StandardDriverKeyBoard>>();

                // keyboard de 0 à 5 étage 1, 6 à 11 étage 2 ...
		for(int j=0; j<6 ;j++){
			Vector<StandardDriverKeyBoard> t = new Vector<StandardDriverKeyBoard>();
			for (int i=0; i<Utils.NB_FLOORS; i++) {
				t.add(new StandardDriverKeyBoard(i,stdLifts.get(j)));
			}
			stdKeyboards.add(t);
                }

                //Création des ecrans
                Vector<Screen> screens = new Vector<Screen>();
                for(int i=0; i<Utils.NB_FLOORS;i++){
                    screens.add(new Screen(i));
                }
                
                //Création du smartSystem
                smartSystem = new SmartSystem(lifts,screens);
                //Création du Simulateur
                simu = new Simulator(keyboards,time,alarm,smartSystem,stdKeyboards,stdLifts);
                //Lancement de l'IHM
                IHMSimulator IHM = new IHMSimulator(simu);
                IHM.setVisible(true);


                // Ajout des observeur des ascenseurs
                it = lifts.iterator();
                while (it.hasNext()) {
                    ((DriverLift)it.next()).addObserver(simu);
                }
                IHM.connectLifts(lifts);
                IHM.connectSimulator();
                
                // Ajout des observeur des ascenseurs cas standard
                it = stdLifts.iterator();
                while (it.hasNext()) {
                    ((StandardDriverLift)it.next()).addObserver(simu);
                }
                IHM.connectStandardLifts(stdLifts);

                //Ajout des observeur des claviers
                it = keyboards.iterator();
                while(it.hasNext()){
                    ((DriverKeyboard)it.next()).addObserver(smartSystem);
                }

                //Ajout des observeur des clavier standard
		for(int i=0;i<6;i++){
			it = stdKeyboards.get(i).iterator();
			while(it.hasNext()){
				StandardDriverKeyBoard t = (StandardDriverKeyBoard)it.next();
				t.addObserver(simu);
			}
		}

                //Ajout des observeur de l'alarme
                alarm.addObserver(smartSystem);
                IHM.connectAlarm(alarm);

                //Ajout des observeur des écrans
                it = screens.iterator();
                while(it.hasNext()){
                    ((Screen)it.next()).addObserver(simu);
                }

                //Création du timer et des Actions
                //Action des Ascenseurs
                it = lifts.iterator();
                while (it.hasNext()) {
                    time.addMethodCall(5000, (DriverLift)it.next(), "update");
                }

                // Action des ascenseurs standard
                it = stdLifts.iterator();
                while (it.hasNext()) {
                    time.addMethodCall(5000, (StandardDriverLift)it.next(), "update");
                }

                //Action du smartSystem
                //time.addMethodCall( 60*60*1000,smartSystem, "updateZones");
                //Action du Simu
                time.addMethodCall(1000,simu,"updateTimeLine");
	    }
	});
    }

}
